package com.healthlink.appcloudcalllib.net;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;

import com.alibaba.fastjson.JSONObject;
import com.healthlink.appcloudcalllib.utils.Out;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class OKCallback implements Callback {
    Handler handler=new Handler();
    Context context;
    public OKCallback(Context context){
        this.context=context;
    }
    @Override
    public void onFailure(Call call, IOException e) {
    Out.out("请求错误："+e.getMessage());
    }

    @Override
    public void onResponse(Call call, Response response) throws IOException {
        String body=response.body().string();
        int code=response.code();
        Out.out(code+"body="+body);
        if (code==200||code==0){
            handler.post(new Runnable() {
                @Override
                public void run() {
                    JSONObject dataObj= JSONObject.parseObject(body);
                    String code=dataObj.getString("code");

                    if (TextUtils.isEmpty(code)){
                        Sucess("ok");
                        return;
                    }
                    try {
                        if (code.equals("0")){
                            String datastr=dataObj.getString("data");
                            Out.outHttpLog("datastr:"+datastr);
                            try {
                                Sucess(datastr);
                            }catch (Exception exception){
                                exception.printStackTrace();
                                Sucess("ok");
                            }
                        }else{
                            try {
                                String msg=dataObj.getString("msg");
                                if (!TextUtils.isEmpty(msg)){
                                    Out.toast(context,msg);
                                }
                            }catch (Exception ex){}

                            Eorror(Integer.parseInt(code));
                        }
                    }catch (Exception exception){
                        Out.out("eee="+exception.getMessage());
                        exception.printStackTrace();
                    }


                }
            });

        }else{
            JSONObject jsonObject=JSONObject.parseObject(body);

            handler.post(new Runnable() {
                @Override
                public void run() {
                    Eorror(code);
                    String msg=jsonObject.getString("message");
                    if (!TextUtils.isEmpty(msg)){
                        if (msg.equals("No call information was found")){
                           Out.out("过滤");
                            Out.toast(context,"通话已不存在！");
                        }else{
                            try {
                                Out.toast(context,msg);
                            }catch (Exception exception){

                            }
                        }

                    }

                }
            });

        }


    }
    public void Sucess(String str){}
    public void Eorror(int code){}
}
